# -*- coding:utf-8 -*-
# @File       :   winOpt.py
# @Author     :   LINING
# @Time       :   2024/7/18 15:09
# @Description : 窗口操作方法
from Handle.handle_log import log


class Win:
    def __init__(self, context):
        self.context = context

    # 窗口最大化
    def win_get_max(self):
        pass

    # 获取所有pages
    def win_get_pages(self):
        try:
            pages = self.context.pages()
            log.log_info('获取所有pages成功')
            return pages
        except Exception as e:
            log.log_error('获取所有pages失败'+repr(e))
            raise e

    # 关闭浏览器
    def win_close(self):
        try:
            self.context.close()
            log.log_info('关闭浏览器成功')
        except Exception as e:
            log.log_error('关闭浏览器失败'+repr(e))
            raise e

    # 打开一个新页面
    def win_open_page(self):
        try:
            page = self.context.new_page()
            log.log_info('打开新页面成功')
            return page
        except Exception as e:
            log.log_error('打开新页面失败'+repr(e))
            raise e

    # 清除浏览器cookie
    def win_clear_cookies(self):
        try:
            self.context.clear_cookies()
            log.log_info('清除浏览器cookie成功')
        except Exception as e:
            log.log_error('清除浏览器cookie失败'+repr(e))
            raise e

    # 清除浏览器授权
    def win_clear_permissions(self):
        try:
            self.context.clear_permissions()
            log.log_info('清除浏览器授权成功')
        except Exception as e:
            log.log_error('清除浏览器授权失败'+repr(e))
            raise e

    # 添加浏览器cookie
    def win_add_cookies(self, cookies):
        """
        :param cookies:[{}]列表里面放字典
        :return:
        """
        try:
            self.context.add_cookies(cookies)
            log.log_info('添加浏览器cookie成功')
        except Exception as e:
            log.log_error('添加浏览器cookie失败'+repr(e))
            raise e

